<?php
/**
 * mysqlBackup
 *
 * backup class for mysql databases
 *
 * @category   Backup Types
 * @package    s3Cinch v1.0
 * @author     Stephen Gray <stephen@zgsolutions.co.uk>
 * @copyright  2010 Stephen Gray
 * @since      Wed 13 Jan 10 22:11
 */
class mysqlBackup extends s3CinchBaseBackup
{
    /**
     * void createArchive ( )
     *
     * run the mysqldump and then archive it
     *
     * @return void
     *
     * @throws s3ArchiveException
     * @access public
     */
    public function createArchive()
    {
        global $config;

        /**
         * check the database exists
         */

        /**
         * if $this->_mainData[2] exists
         * then we are trying to use a specific 
         * mysql host
         */
        if (isset($this->_mainData[2]) && count(explode(',', $this->_mainData[2])) == 3)
        {
            $mysqlParts = explode(',', $this->_mainData[2]);
            $this->log('Using custom MySQL server; host: '.$mysqlParts[0].', username: '.$mysqlParts[1].', password: '.$mysqlParts[2].'.');
            if (!$conn = @mysql_connect($mysqlParts[0], $mysqlParts[1], $mysqlParts[2]))
            {
                throw new s3ArchiveException('Could not connect to MySQL server.');
            }
        }
        else
        {
            $this->log('Using default MySQL server.');
            if (!$conn = @mysql_connect($config['mysql']['host'], $config['mysql']['username'], $config['mysql']['password']))
            {
                throw new s3ArchiveException('Could not connect to MySQL server.');
            }
        }

        $dbArray = mysql_list_dbs($conn);
        $foundDB = false;
        while ($row = mysql_fetch_assoc($dbArray))
        {
            if ($row['Database'] == $this->_mainData[1])
            {
                $foundDB = true;
                break;
            }
        }

        if (!$foundDB)
        {
            throw new s3ArchiveException('Database '.$this->_mainData[1].' does not exist.');
        }

        /**
         * make a folder for this in the archives
         */
        $this->_checkForFolder($this->_mainData[1]);

        $this->log('Creating dump for database '.$this->_mainData[1]);

        $dumpFileName = $config['dirs']['archives'].'/'.$this->_mainData[1].'/'.date($config['general']['file_date_format']).'-'.$this->_mainData[1].'.sql';
        $command = str_replace('{target}', $dumpFileName, $config['general']['archive_mysql_command']);
        $command = str_replace('{source}', $this->_mainData[1], $command);
        $this->log('Command: '.$command);

        $output = shell_exec($command);
        if (!empty($output))
        {
            throw new s3ArchiveException('Problem creating dump, skipping.');
        }

        $this->log('Creating archive for sql file '.$dumpFileName);

        $this->_bucketFileFolder = $this->_mainData[1];

        /**
         * the command we're going to run
         */
        $archiveFileName = $config['dirs']['archives'].'/'.$this->_mainData[1].'/'.date($config['general']['file_date_format']).'-'.$this->_mainData[1].'.tar.gz';
        $command = str_replace('{target}', $archiveFileName, $config['general']['archive_file_command']);
        $command = str_replace('{source}', $dumpFileName, $command);
        $this->log('Command: '.$command);

        /**
         * run it
         */
        $output = shell_exec($command);
        if (!empty($output))
        {
            throw new s3ArchiveException('Error running archive command: '.$output);
        }
        else
        {
            $this->log('Successfully created file '.$archiveFileName);
            $this->_archiveFilePath = $archiveFileName;
        }
    }
}
